import java.util.*;

/**
 * @author LKQ
 * @date 2021/12/11 10:29
 * @description 使用迭代算法, Deque双向队列，可实现栈的特性，而Queue为单向队列，为队列先进先出的特性，适合广度优先
 */
public class Solution1 {
    public static void main(String[] args) {

    }

    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        if (root == null) {
            return ans;
        }
        Deque<TreeNode> stk = new LinkedList<TreeNode>();
        while (root != null || !stk.isEmpty()) {
            while (root != null) {
                stk.push(root);
                ans.add(root.val);
                root = root.left;
            }
            root = stk.pop();
            root = root.right;
        }
        return ans;
    }
}
